package com.salton123.soulove.sailfish.video.ui.fm

import android.os.Bundle
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.observe
import androidx.recyclerview.widget.SimpleItemAnimator
import com.alibaba.android.arouter.facade.annotation.Route
import com.salton123.log.XLog
import com.salton123.soulove.api.RouterManager
import com.salton123.soulove.app.R
import com.salton123.soulove.common.Constants
import com.salton123.soulove.common.adapter.SmartRefreshDelegate
import com.salton123.soulove.sailfish.db.SailfishDatabase
import com.salton123.soulove.sailfish.video.ui.adapter.VideoCompressionAdapter
import com.salton123.soulove.utils.Utils
import com.salton123.ui.base.BaseFragment
import kotlinx.android.synthetic.main.fragment_video_list.*
import kotlinx.coroutines.launch

/**
 * Time:1/17/22 4:25 PM
 * Author:
 * Description:
 */

@Route(path = Constants.Router.Video.LIST)
class VideoListFragment : BaseFragment(), SmartRefreshDelegate {
    override fun isAsyncContentView(): Boolean = false
    private val videoHomeVerticalAdapter by lazy { VideoCompressionAdapter() }
    override fun loadData(isRefresh: Boolean) {
    }

    override fun getLayoutId(): Int = R.layout.fragment_video_list

    override fun initViewAndData(savedInstanceState: Bundle?) {
        initRefreshDelegate(recyclerView, smartRefreshLayout, videoHomeVerticalAdapter)
        recyclerView.setHasFixedSize(true)
        (recyclerView.itemAnimator as SimpleItemAnimator).supportsChangeAnimations = false
        setAdapterData()
    }

    private val TAG = "VideoListFragment"
    private fun setAdapterData() {
        lifecycleScope.launch {
            val param = Utils.sortParam()
            XLog.i(TAG, "param:$param")
            SailfishDatabase.Impl.videoDao().loadAll(param.first, param.second)
                .observe(viewLifecycleOwner) {
                    recyclerView.adapter = videoHomeVerticalAdapter
                    videoHomeVerticalAdapter.setOnItemClickListener { adapter, view, position ->
                        RouterManager.videoCompress(activity(), videoHomeVerticalAdapter.getItem(position).filePath)
                    }
                    videoHomeVerticalAdapter.setNewInstance(it)
                }
        }
    }
}